<?php
declare(strict_types=1);

namespace app\api\controller;

use app\api\service\ShopOrder as ShopOrderService;
use app\common\controller\Api;
use app\common\model\delivery\Coupon;
use app\common\model\delivery\CouponList;
use app\common\model\delivery\Customer;
use app\common\model\delivery\GoodsSpec;
use app\common\model\delivery\ShopGoods;
use app\common\model\delivery\ShopOrder;
use think\annotation\route\Get;
use think\annotation\route\Group;
use think\annotation\route\Post;

#[Group("shop")]
class Shop extends Api
{

    #[Get('orderlist')]
    public function orderlist()
    {
        $param=$this->request->get();
        $offest=((int)$param['page']-1)*15;
        $list=ShopOrder::with(['goods'])->where(function ($query) use ($param){
            $customer=Customer::find($param['customer_id']);
            $query->where('channel_id','=',$customer->channel_id);
            $query->where('user_id','=',$this->auth->id);
            $query->where('is_pay','=',1);
        })->limit($offest,15)->order('id desc')->select();
        $this->success('',$list);
    }

    #[Get('goodslist')]
    public function goodslist()
    {
        $page=$this->request->get('page/d',1);
        $offest=($page-1)*15;
        $list=ShopGoods::with(['coupon'])->where(function ($query){
            $query->where('status','=','normal');
            $keywords=$this->request->get('keywords');
            if($keywords){
                $query->where('name','like',"%{$keywords}%");
            }
            $channel_id=$this->request->get('channel_id/d',3);
            $query->where('channel_id','=',$channel_id);
        })->limit($offest,15)->order('id desc')->select();
        $this->success('',[
            'res'=>$list,
            'user'=>$this->auth->userinfo()
        ]);
    }

    #[Post('buy')]
    public function buy()
    {
        $postdata=$this->request->post();
        $postdata['user']=$this->auth->userinfo();
        try {
            $res=ShopOrderService::newInstance($postdata)->createOrder();
        }catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    #[Get('coupon')]
    public function coupon()
    {
        $page=$this->request->get('page/d',1);
        $offest=($page-1)*15;
        $list=CouponList::withJoin(['coupon'],'inner')->where(function ($query){
            $query->where('coupon_list.customer_id','=',$this->request->get('customer_id'));
            $query->where('coupon.deletetime','=',null);
        })->limit($offest,15)->order('id desc')->select();
        $spec=GoodsSpec::column('unit','goods_id');
        $now=time();
        foreach ($list as $key => $value) {
           if($list[$key]['coupon'] && $list[$key]['coupon']['type']==Coupon::COUPONTYPE('商品券')){
               $goods_id=$list[$key]['coupon']['goods_id'];
               $list[$key]['unit']=isset($spec[$goods_id])?$spec[$goods_id]:'次';
           }
           $list[$key]['status']=0;
           if($value['total']<=$value['take']){
                $list[$key]['status']=1;
           }
           if($value['total']>$value['take'] && $now>$value['expire_time']){
               $list[$key]['status']=-1;
           }
           $use_limit_type=$value['coupon']->use_limit_type;
           if($use_limit_type=='week'){
                $use_limit_week=explode(',',$value['coupon']->use_limit_week);
                if(!in_array(date('w',$now),$use_limit_week)){
                    $list[$key]['status']=-2;
                }
           }
           if($use_limit_type=='date'){
                $use_limit_date=$value['coupon']->use_limit_date;
                $t1=strtotime(substr($use_limit_date,0,10).' 00:00:00');
                $t2=strtotime(substr($use_limit_date,13,10).' 23:59:59');
                if($now<$t1 || $now>$t2){
                    $list[$key]['status']=-3;
                }
           }
           $list[$key]['expire_time']=date('Y-m-d H:i',$value['expire_time']);
        }
        $this->success('',$list);
    }
}